﻿using System.Collections.Generic;
using COC.Character;
using COC.Threading;
using COC.Tools.Pathing;
using UnityEngine;
using System.Collections;

namespace COC.Tools.Pathing
{
    public class AIPathFinding : BaseBehaviourSupport
    {
        public PathFindingPool pool;
        private float mWidth, mHeight,mTileSize,mOffset = 0.0f;

        private CTile[,] obstacleMap;
        private Stack<WorkEventItem> list;
        public override void OnAwake()
        {
        }
        public override void OnStart()
        {

         


        }
        public void Setup(bool background, int size, float width, float height,float tileSize,float offset, CTile[,] obstacles)
        {
            mWidth = width;
            mHeight = height;
            mTileSize = tileSize;
            mOffset = offset;
            obstacleMap = obstacles;
            if(list==null)
                list = new Stack<WorkEventItem>();
            initialPool();
        }
        public void initialPool()
        {
            if (pool == null)
                pool = PathFindingPool.getInstance(true, 5, mWidth, mHeight,mTileSize,mOffset,obstacleMap);
            
         
        }

        public void Lunch()
        {
            pool.Start();
        }
        public void pushPath(CPath path)
        {
            WorkEventItem pathEvent=new WorkEventItem();
            pathEvent.type = CEvent.CEventAction.ACTION_PATH_FINDING;
            pathEvent.arg = path;
            PathFindingPool.mDoQueue.PushEvent(pathEvent);
        }

        public void refreshPath(CPath path)
        {
            path.STATE = PathInfo.PathState.REQUEST_PATH;
        }
        void Update()
        {

        }
        public PathInfo.PathState searchPath(CPath path)
        {
            return pool.getPathfindingMath().searchingPath(path);
        }
        public override void OnDestroy()
        {

            {
                if (pool != null)
                {
                    pool.Cancel(true);
                    while (pool.isCancel())
                    {
                        pool = null;

                        Destroy(this);
                    }
                }
            }

        }

       public  Vector3 ReadPath2(int id, float x, float z, float y)
        {
            return pool.getPathfindingMath().ReadPath2(id, x, z, y);
        }
    }


}
